package com.zhouyibike.uu.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.entity.PaiMingResult;
import com.zhouyibike.uu.ui.activity.jvbaoandpaiming.PaimingActivity;
import com.zhouyibike.uu.ui.view.TXCircleImageView;

import java.util.List;

/**
 * Created by cc on 2017/5/25.
 */

public class PaimingAdapter extends BaseAdapter {
    private Context context;
    private List<PaiMingResult.DataBean> list;
    private PaimingActivity activity;

    public PaimingAdapter(Context context, List<PaiMingResult.DataBean> list) {
        this.context = context;
        this.list = list;
        activity= (PaimingActivity) context;
    }
    public void setList(List<PaiMingResult.DataBean> list){
        this.list = list;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return list.size()-1;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position+1);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView= LayoutInflater.from(context).inflate(R.layout.item_paiming,null);
        TXCircleImageView txCircleImageView= (TXCircleImageView) convertView.findViewById(R.id.txciv_paimingitem_myhead);
        TextView tv_name= (TextView) convertView.findViewById(R.id.tv_paimingitem_name);
        TextView tv_pm= (TextView) convertView.findViewById(R.id.tv_paimingitem_pm);
        TextView tv_mails= (TextView) convertView.findViewById(R.id.tv_paimingitem_mails);
        TextView tv_zan= (TextView) convertView.findViewById(R.id.tv_paimingitem_zans);
        ImageView iv_xin= (ImageView) convertView.findViewById(R.id.iv_paimingitem_xin);
        tv_name.setText(list.get(position+1).getName());
        try {
            Picasso.with(context).load(list.get(position+1).getHeadImage()).networkPolicy(NetworkPolicy.NO_CACHE,NetworkPolicy.NO_STORE).into(txCircleImageView);
        }catch (Exception e){

        }
        if(list.get(position+1).getIndex()<10){
            tv_pm.setText("0"+list.get(position+1).getIndex());
        }else{
            tv_pm.setText(""+list.get(position+1).getIndex());
        }
        tv_mails.setText(list.get(position+1).getMileage()+"KM");
        tv_zan.setText(list.get(position+1).getPoints()+"");
        if(list.get(position+1).isHasPraised()){
            iv_xin.setImageResource(R.mipmap.zan_t);
        }else{
            iv_xin.setImageResource(R.mipmap.zan_f);
        }
        iv_xin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!list.get(position+1).isHasPraised())
                     activity.dianzan(list.get(position+1).getAccountId()+"");
            }
        });
        return convertView;
    }
}
